#!/bin/bash
#
# ircDDBGateway startup script
# John Hays (K7VE) - john@hays.org
# Hans-J. Barthen (DL5DI) - dl5di@gmx.de
#
# chkconfig: 2345 99 01
# description: Starts the G4KLX ircDDBGateway
# config: /etc/sysconfig/ircddbgateway
#

if [ $UID -ne 0 ]; then
	echo
	     echo "ERROR:  This script must be run as the root user!"
	     echo "        Please use 'su' or log in as root and try again."
	     echo
	     exit 1
fi

me=ircddbgateway

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 1

# Source the configuration
[ -f /etc/sysconfig/$me ] && . /etc/sysconfig/$me

dostart() {

    echo -n "Starting $me:"
    daemon $DAEMON_PATH $DAEMON_OPTIONS 
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$me
    echo
    sleep 2
    # create a watchdog entry to the crontab
    echo "*/10 * * * * root ps aux | grep $DAEMON_PATH | grep -vq grep || /etc/init.d/$me start" > /etc/cron.d/$me
    echo "The gateway is up and running"
    if [ "${IRCDDBGATEWAY_DDENABLED}" = "yes" ]; then
    	echo " - wait until DD-mode interface is up"
        count=0
        while ! grep -q $IRCDDBGATEWAY_DDDEV /proc/net/dev; do
            sleep 1
            echo -n "."
            if [ $count -gt 120 ]; then
                echo "Error!! DD-interface $IRCDDBGATEWAY_DDDEV was not created"
                exit 1
            fi
            let count=$count+1
        done
        echo "OK"
        echo " - configure dd-mode network device $IRCDDBGATEWAY_DDDEV "
        /sbin/ifconfig $IRCDDBGATEWAY_DDDEV $IRCDDBGATEWAY_DDIP netmask $IRCDDBGATEWAY_DDMASK
        echo 1 > /proc/sys/net/ipv4/ip_forward
        if [ "${IRCDDBGATEWAY_DDDHCP}" = "yes" ]; then
            echo " - start dhcpd "
            /sbin/service dhcpd start
            sleep 3
            [ -x /usr/local/bin/dd-services.sh ] && /usr/local/bin/dd-services.sh
        fi
    fi
}

dostop() {
    
    echo -n "Stopping $me "
    if [ "${IRCDDBGATEWAY_DDENABLED}" = "yes" -a  "${IRCDDBGATEWAY_DDDHCP}" = "yes" ]; then
        echo " - dhcp for dd-mode is enabled, stop it "
        /sbin/service dhcpd stop
        sleep 15
    fi                    
    killproc $DAEMON_PATH
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$me
    # remove watchdog entry from crontab
    rm -f /etc/cron.d/$me                
    echo

}

dorestart() {
    dostop
    sleep 2
    if [ "${IRCDDBGATEWAY_DDENABLED}" = "yes" -a  "${IRCDDBGATEWAY_DDDHCP}" = "yes" ]; then
	sleep 15
    fi
    dostart
}	

# See how we were called.
case "$1" in
    start)
	dostart
	;;
    stop)
	dostop
	;;
    restart)
	dorestart
	;;
    status)
	status $DAEMON_PATH
	;;
    *)
	echo -e $"\nUsage: \n\t$0 {option}"
	echo -e "\n\tOptions:\n"
	echo -e "\trestart       - restarts $me\n"
	echo -e "\tstart         - starts $me\n"
	echo -e "\tstatus        - display status\n"
	echo -e "\tstop          - stops $me\n"
	echo -e "\n"
	exit 1
esac

exit $RETVAL
